<template>
  <div class="pswForm">
    <el-form ref="pswForm" :rules="formRules" class="small-space" :model="pswData" :label-position="'right'"
               >
      <dym-form :editModel="editModel" :componentList="componentList" :formValue.sync="pswData"
        ></dym-form>
    </el-form>
  </div>
</template>
<script>
  import dymForm from '@/components/element/dymForm'
  import { sysUser } from '@/api/platformApi'
  import { Encrypt } from '@/utils/aesUtil.js'
  export default {
    components: {
      dymForm
    },
    props: {
      form: {
        type: Object,
        default: function () {
          return {}
        }
      }
    },
    data () {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'))
        } else {
          if (this.pswData.checkpass !== '') {
            this.$refs.pswForm.validateField('checkpass')
          }
          callback()
        }
      }
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'))
        } else if (value !== this.pswData.password) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      }
      return {
        checkUserFlag: false,
        editModel: 'E',
        componentList: [
          {
            label: '原始密码',
            prop: 'oldpass', // 表单验证传入的值
            labelWidth: '90px',
            componentName: 'passwd',
            type: 'password',
            cols: [24, 24, 24, 24],
            placeholder: '请输入',
            value: 'oldpass' // 字段显示的值
          },
          {
            label: '新密码',
            prop: 'password', // 表单验证传入的值
            labelWidth: '90px',
            componentName: 'el-input',
            type: 'password',
            cols: [24, 24, 24, 24],
            placeholder: '请输入',
            value: 'password'
          },
          {
            label: '重复新密码',
            prop: 'checkpass', // 表单验证传入的值
            labelWidth: '90px',
            componentName: 'el-input',
            cols: [24, 24, 24, 24],
            type: 'password',
            placeholder: '请输入',
            value: 'checkpass'
          }
        ],
        formRules: {
          oldpass: [],
          password: [{ validator: validatePass, trigger: 'blur' }],
          checkpass: [{ validator: validatePass2, trigger: 'blur' }]
        },
        pswData: {
          oldpass: '',
          password: '',
          checkpass: ''
        }
      }
    },
    methods: {
      save () {
        let d = {
          id: this.$store.state.user.userId,
          password: this.pswData.oldpass
        }
        let e = {
            str: Encrypt(JSON.stringify(d))
        }
        sysUser.checkUserPass(e).then(res => {
          if (res.code === -1) {
            this.error('原始密码填写错误')
          } else if (res.code === 0) {
            this.$refs['pswForm'].validate((valid) => {
              if (valid) {
                this.form.password = this.pswData.password
                // this.$emit('submitForm', this.form)
                sysUser.editPass(this.form).then(res => {
                  this.success(res.msg)
                })
              } else {
                this.error('表单填写错误')
                return false
              }
            })
          }
        })
      }
    }
  }
</script>

<style>
  .pswForm {
    width: 40%;
    margin: 40px auto 0px;
  }
  .pswForm .el-form-item {
    margin-bottom: 20px!important;
  }
</style>
